import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_architecture/config/app_config.dart';
import 'package:flutter_architecture/constant/env_enum.dart';
import 'package:flutter_architecture/routes/routes.dart';
import 'package:flutter_architecture/states/providers.dart';
import 'package:flutter_architecture/utils/app_update_util.dart';
import 'package:flutter_architecture/utils/log_util.dart';
import 'package:flutter_architecture/views/root_app_page.dart';
import 'package:provider/provider.dart';
import 'package:logger/logger.dart';

void main() async {
  //App启动完成后，设置App屏幕方向
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]).then((value) => {
        runApp(MultiProvider(
          providers: providers,
          child: RootAppPage(
            globalRouteKey: AppConfig.globalRouteKey,
          ),
        ))
      });
  init();
}

/// App信息初始化
/// 1. 设定运行环境
/// 2.设置错误页面
/// 3.路由初始化
/// 4.app 更新
/// 初始化状态栏样式
/// todo 国际化
/// todo 多主题
/// todo 自定义字体
/// todo fluro 进一步细节化处理
/// todo 屏幕适配工具初始化优化
void init() {
  AppConfig.env = Env.DEV;
  AppConfig.initCustomErrorPage();
  AppConfig.globalRouteKey = AppConfig.globalRouteKey;
  AppConfig.initUpdate();
  AppConfig.checkAppUpdate();
  AppConfig.initSystemUIOverlayStyle();
  //todo 路由初始化
  Routes.setUpRoutes();
  log(
      level: Level.info,
      fileName: 'main.dart',
      functionName: 'init',
      message: '当前环境：${AppConfig.env}、错误页面设置、全局路由Key初始化、App更新初始化');
}
